package com.hancai.service.impl;

import com.alibaba.excel.EasyExcel;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.hancai.config.listener.UploadDataListener;
import com.hancai.constant.Constants;
import com.hancai.mapper.TBarrageMapper;
import com.hancai.model.TBarrage;
import com.hancai.service.BarrageService;
import jakarta.annotation.Resource;
import org.springframework.stereotype.Service;

import java.io.InputStream;
import java.util.List;

/**
 * @author 涵菜
 * @version 1.0
 */
@Service
public class BarrageServiceImpl implements BarrageService {

    @Resource
    private TBarrageMapper tBarrageMapper;
    @Override
    public PageInfo<TBarrage> getBarrageByPage(Integer current, String keyword) {
        // 1.设置PageHelper
        PageHelper.startPage(current, Constants.PAGE_SIZE);
        // 2.查询
        List<TBarrage> list = tBarrageMapper.selectBarrageByPage(keyword);
        // 3.封装分页数据到PageInfo
        PageInfo<TBarrage> info = new PageInfo<>(list);
        return info;
    }

    @Override
    public void changeStatus(Integer id, Integer status) {
        tBarrageMapper.changeStatus(id,status);
    }

    @Override
    public int delBarrageById(Integer id) {
        return tBarrageMapper.deleteByPrimaryKey(id);
    }

    @Override
    public int batchDelUserIds(List<String> idList) {
        return tBarrageMapper.deleteByIds(idList);
    }

    @Override
    public void importExcel(InputStream inputStream) {
        /**三个参数
         * 第一个参数是要读取的Excel文件
         * 第二个参数是Excel模板类
         */
        //链式编程，3个参数, 第一个参数是要读取的Excel文件，第二个参数是Excel模板类，第三个参数是文件读取的监听器
        EasyExcel.read(inputStream, TBarrage.class, new UploadDataListener(tBarrageMapper))
                .sheet()
                .doRead();
    }
}
